package com.example.camerademo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.example.camerademo.util.SharedPreferencesUtils;

/**
 * author: Liulei
 * created on: 2021/3/31 18:06
 * description:
 */
public class RelativeLayoutDemoActivity extends Activity {

    private static String USERNAME = "username";
    private static String PASSWORD = "password";

    private EditText username;
    private EditText password;

    private Button bt_username_clear;  //清楚用户账号输入框的内容的button
    private Button bt_pwd_eye;  // 显示/隐藏 密码输入框的内容
    private Button bt_pwd_clear;  //清楚用户密码输入框的内容的button
    private Button bt_save; // 保存信息

    private View.OnClickListener listener = new View.OnClickListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_username_clear:
                    username.setText("");
                    break;
                case R.id.bt_pwd_eye:
                    System.out.println(password.getInputType());
                    if (password.getInputType() == 145) {
                        password.setInputType(129);
                    } else if (password.getInputType() == 129) {
                        password.setInputType(145);
                    }
                    break;
                case R.id.bt_pwd_clear:
                    password.setText("");
                    break;
                case R.id.bt_save:
                    SharedPreferencesUtils.save(RelativeLayoutDemoActivity.this, USERNAME, username.getText());
                    SharedPreferencesUtils.save(RelativeLayoutDemoActivity.this, PASSWORD, password.getText());
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relative_layout_demo);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        bt_username_clear = findViewById(R.id.bt_username_clear);
        bt_pwd_eye = findViewById(R.id.bt_pwd_eye);
        bt_pwd_clear = findViewById(R.id.bt_pwd_clear);
        bt_save = findViewById(R.id.bt_save);

        bt_username_clear.setOnClickListener(listener);
        bt_pwd_eye.setOnClickListener(listener);
        bt_pwd_clear.setOnClickListener(listener);
        bt_save.setOnClickListener(listener);

        // 获取数据
        String usernameStr = (String) SharedPreferencesUtils.get(RelativeLayoutDemoActivity.this, USERNAME, "");
        String passwordStr = (String) SharedPreferencesUtils.get(RelativeLayoutDemoActivity.this, PASSWORD, "");
        username.setText(usernameStr);
        password.setText(passwordStr);
    }
}
